﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;


namespace TradeForm
{
    public static class Utility
    {
        private static DataDBDataContext dbcntxt = new DataDBDataContext
                 (ConfigurationManager.ConnectionStrings["TradeForm.Properties.Settings.dbConnectionString"].ConnectionString);
        public static string UserID;

        public static EncryptOperate encrypt = new EncryptOperate();

        public static bool PwdIsCorrect(string uid, string pwd)
        {
            decimal i = 0;
            var usr = from user in dbcntxt.Users
                      where user.UserID == uid && user.Pwd == encrypt.EncryptIt(pwd)
                      select user;
            if (usr != null && usr.Count() > 0)
            {
                return true;
            }
            return false;
        }

        public static bool ModifyPwd(string uid, string oldpwd, string NewPwd)
        {
            var usr = from user in dbcntxt.Users
                      where user.UserID == uid && user.Pwd == encrypt.EncryptIt(oldpwd)
                      select user;

            if (usr != null && usr.Count() > 0)
            {
                try
                {
                    foreach (var user in usr)
                    {
                        user.Pwd = encrypt.EncryptIt(NewPwd);
                    }
                    dbcntxt.SubmitChanges();
                }
                catch
                {
                    return false;
                }
                return true;
            }
            return false;
        }


    }
}
